package edu.dongthang.server;
import java.awt.Robot;
import java.awt.MouseInfo;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/**
 * @author thangnd
 *
 */
public class AutoBot {
	private Robot robot;
	private int mouse_sensitivity = 1;
	private boolean note = false;
	int lastXpos = 0;
	int lastYpos = 0;
	int lastScrollY;
	
	boolean special_key = true;
	
	public BufferedImage img;
	
	public AutoBot(){
		try{ robot = new Robot(); }
		catch(Exception e){ System.out.println("Couldn't Create robot");}
	}
	
	/**
	 * @param event
	 */
	public void handleMessage(String event){

		try{
			String[] tokens = event.split("!!");
			
			if(tokens[0].equals("CLICK")){
				controlSlide(tokens[1]);				
				f5Press(tokens[1]);
				escPress(tokens[1]);
			}else if(tokens[0].equals("NOTE")){
				
			}
			
			
			else if(tokens[0].equals("Mouse Sensitivity")){
				mouse_sensitivity = Integer.parseInt(tokens[1]);
			}
			
			else if(tokens[0].equals("MOVE")){
				int currXpos = Integer.parseInt(tokens[1]);
				int currYpos = Integer.parseInt(tokens[2]);
				
				int deltaX = (lastXpos - currXpos) * -1;
				int deltaY = (lastYpos - currYpos) * -1;
				
				moveMouse(deltaX, deltaY);
				
				lastXpos = currXpos;
				lastYpos = currYpos;
				//screenCap();
			}
			else if(tokens[0].equals("DOWN")){
				lastXpos = Integer.parseInt(tokens[1]);
				lastYpos = Integer.parseInt(tokens[2]);
			}
			else if(tokens[0].equals("UP")){
				int deltaTime = Integer.parseInt(tokens[2]) - Integer.parseInt(tokens[1]);
				if(deltaTime < 250) clickMouse("LEFT");
			}
			
			else if(tokens[0].equals("SCROLL")){
				if(tokens[1].equals("DOWN"))
					lastScrollY = lastYpos;
				
				else if(tokens[1].equals("MOVE")){
					int currYpos = Integer.parseInt(tokens[3]);
					
					int deltaY = currYpos - lastScrollY;
					int tolerance = 10;
					
					if	(deltaY > tolerance){
						robot.mouseWheel(1);
						lastScrollY = currYpos;
					}
					
					if (deltaY < -1 * tolerance){
						robot.mouseWheel(-1);
						lastScrollY = currYpos;
					}					
					//screenCap();
				}
			}
			
//			else if(tokens[0].equals("KEY")){
//				type(tokens[1].charAt(0));
//			}
			
			else if(tokens[0].equals("S_KEY")){
				
				if(special_key){
					int key;
					int key_code = Integer.parseInt(tokens[1]);
					
					switch(key_code){
						case 66: key = KeyEvent.VK_ENTER; break;
						case 67: key = KeyEvent.VK_BACK_SPACE; break;
						default: key = -1; break;
					}
					keyBoardPress(key);
					special_key = false;
				}
				else special_key = true;
				
				
			}
			
			else if(tokens[0].equals("MOUSE")){
				clickMouse(tokens[1]);
			}
			
			else
				System.out.println("else message" + tokens[0] +" "+ tokens[1]);
		}catch(Exception e){}
	}
	
	public void screenCap(){
		Rectangle captureSize = new Rectangle(lastXpos, lastYpos, 500, 500);
		img = robot.createScreenCapture(captureSize);
	}
	
	public BufferedImage getScreenCap(){
		return img;
	}
	
	public void moveMouse(int x, int y){
		//get the current position of the mouse cursor
		int current_x_local = MouseInfo.getPointerInfo().getLocation().x;
		int current_y_local = MouseInfo.getPointerInfo().getLocation().y;
		
		//move the mouse relative to the current position
		robot.mouseMove(current_x_local + x*mouse_sensitivity,
						current_y_local + y*mouse_sensitivity);
	}
	
	public void clickMouse(String which){
		int button;
		if(which.equals("LEFT")){
			button = InputEvent.BUTTON1_MASK;
			System.out.print("click left mouse");
			//button = InputEvent.BUTTON3_MASK;
			//System.out.print("click right mouse");
		robot.mousePress(button);
		robot.mouseRelease(button);}
	}
	
	public void controlSlide(String what){
		int key;
		if(what.equals("LEFT"))
			key = KeyEvent.VK_UP;
		else 
			key = KeyEvent.VK_DOWN;
		keyBoardPress(key);
	}
	
	public void f5Press(String what){
		int key;
		if(what.equals("F5")){
			key = KeyEvent.VK_F5;
			keyBoardPress(key);
		}
			
	}
	
	public void sendNote(String what){
		if(what.equals("NOTE")){
			note = true;
		}
	}
	public void escPress(String what){
		int key;
		if(what.equals("ESC")){
			key = KeyEvent.VK_ESCAPE;
			keyBoardPress(key);
		}
			
	}
	public void scrollMouseWheel(int notches){
		robot.mouseWheel(notches);
	}
	
	public void keyBoardPress(int key){
		try{
			robot.keyPress(key);
			robot.keyRelease(key);
		}catch(Exception e){}
	}
	
	public void keyBoardPress(int key, int key2){
		try{
			robot.keyPress(key);
			robot.keyPress(key2);
			
			robot.keyRelease(key2);
			robot.keyRelease(key);
		}catch(Exception e){}
	}
}
